<?php
/**********************************************************************************
WIKINDX: Bibliographic Management system.
Copyright (C)

This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program;
if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

The WIKINDX Team 2007
sirfragalot@users.sourceforge.net

**********************************************************************************/
/*****
* BROWSECOLLECTION class
*
* Browse collections as a 'tag cloud'
*
* 
$Header: /cvsroot/wikindx/wikindx3/core/browse/BROWSECOLLECTION.php,v 1.13 2007/12/03 04:18:37 sirfragalot Exp $
*****/
class BROWSECOLLECTION
{
// Constructor
	function BROWSECOLLECTION($db, $vars)
	{
		$this->db = $db;
		$this->vars = $vars;
		include_once("core/html/MISC.php");
		include_once("core/html/HTML.php");
		$this->html = new HTML();
		include_once("core/messages/MESSAGES.php");
		$this->messages = new MESSAGES();
		include_once("core/template/TEMPLATE.php");
		$this->template = new TEMPLATE('content');
		include_once("core/browse/BROWSECOMMON.php");
		$this->common = new BROWSECOMMON($this->db);
		include_once("core/session/SESSION.php");
		$this->session = new SESSION();
	}
// display types of collections
	function display1()
	{
		$this->template->setVar('heading', $this->messages->text("heading", "browseCollection"));
		$recordset = $this->db->select(array('WKX_collection'), 'collectionType', 
			' GROUP BY ' . $this->db->formatField('collectionType'));
		if(!$this->db->numRows($recordset))
		{
			$this->template->setVar('body', $this->messages->text("misc", "noCollections"));
			return $this->template->process();
		}
// Add 'ALL' to array
		$collections[0] = $this->messages->text("collection", 'all');
		while($row = $this->db->loopRecordSet($recordset))
		{
			if(!$row['collectionType'])
				continue;
			$collections[$row['collectionType']] = $this->messages->text("collection", $row['collectionType']);
		}
		$pString = FORM::formHeader("browseCollection2");
		$pString .= FORM::selectFBoxValue($this->messages->text("collection", 'chooseTypeBrowse'), 
			"collectionType", $collections, 10);
		$pString .= MISC::br() . "&nbsp;" . MISC::br();
		$pString .= MISC::p(FORM::formSubmit("Proceed"), FALSE, "right");
		$pString .= FORM::formEnd();
		$this->template->setVar('body', $pString);
		return $this->template->process();
	}
// display collections
	function display2()
	{
		if(!array_key_exists('collectionType', $this->vars)) // set default to 'ALL'
		{
			$collectionType = 0;
			$queryString = "action=browseCollection2";
		}
		else
		{
			$collectionType = $this->vars['collectionType'];
			$queryString = "action=browseCollection2" . "&collectionType=" . $collectionType;
		}
		$this->template->setVar('heading', $this->messages->text("heading", "browseCollection"));
		$this->sum = $this->collection = array();
		$this->getCollections($collectionType);
		if(empty($this->collections))
		{
			$pString = MISC::p("&nbsp;") . $this->messages->text("misc", "noCollections");
			$this->template->setVar('body', $pString);
			return $this->template->process();
		}
		natcasesort($this->sum);
		natcasesort($this->collections);
		$this->collections = $this->common->paging($this->vars, $this->collections);
		$bib = $this->bib ? MISC::p($this->bib, "hint") : FALSE;
		$pString = $bib . $this->common->linksInfo() . 
			MISC::pBrowse($this->process()/*, "center"*/);
		$this->template->setVar('body', $pString);
		$this->template->setVar('paging', $this->common->pagingLinks($queryString));
		return $this->template->process();
	}
// Process and display results
	function process()
	{
		$lowestSum = current($this->sum);
		$highestSum = end($this->sum);
		foreach($this->collections as $id => $name)
		{
			$name = $this->html->codeToHtml($name);
			//$colour = $this->common->colourText($lowestSum, $highestSum, $this->sum[$id]);
			//$size = $this->common->sizeText($lowestSum, $highestSum, $this->sum[$id]);
			$links[] = MISC::aBrowse($colour, $size, $name, "index.php?action=listCollectionProcess" . 
				htmlentities("&id=" . $id)) . "&nbsp;[" . $this->sum[$id] . "]";
		}
		return join("<br> ", $links);
		//return join("&nbsp; ", $links);
	}
// Get collections from db
	function getCollections($collectionType)
	{
		include_once("core/collection/COLLECTION.php");
		$this->collection = new COLLECTION($this->db);
		include_once("core/collection/COLLECTIONMAP.php");
		$this->map = new COLLECTIONMAP();
		$join = ' LEFT JOIN ' . $this->db->formatTable('WKX_collection') . ' ON ' . 
			$this->db->formatField('WKX_collection.id') . '=' . 
			$this->db->formatField('collection');
		list($condition, $bib) = $this->common->userBibCondition('WKX_resource_misc');
		if($condition)
			$condition = " WHERE " . $condition;
		if($collectionType)
		{
			if(!$condition)
				$condition = ' WHERE ' . $this->db->formatField('collectionType') . '=' . 
			$this->db->tidyInput($collectionType);
			else
				$condition .= ' AND ' . $this->db->formatField('collectionType') . '=' . 
				$this->db->tidyInput($collectionType);
		}
		$condition = $join . $condition;
		$recordset = $this->db->select(array('WKX_resource_misc'), 
			array('WKX_resource_misc.id', 'collection', 'collectionType', 'collectionTitle', 
			'collectionTitleShort', 'collectionString'), $condition);
		while($row = $this->db->loopRecordSet($recordset))
			$this->collate($row);
		if($bib)
			$this->bib = $this->messages->text("user", "bibliography") . ": " . $bib;
		else if($this->session->getVar('setup_multiUser'))
			$this->bib = $this->messages->text("user", "bibliography") . ": " . 
				$this->messages->text("user", "masterBib");
		else
			$this->bib = FALSE;
	}
// Add collections to array and sum totals
	function collate($row)
	{
		if(!$row['collection'])
			return;
// Get totals for each collection
		$recordset = $this->db->selectCount(array('WKX_resource_misc'), 'collection', 
			' WHERE ' . $this->db->formatField('collection') . '=' . $this->db->tidyInput($row['collection']));
		$row2 = $this->db->fetchRow($recordset);
		$this->sum[$row['collection']] = $row2['count'];
		$short = $row['collectionTitleShort'] ? 
			" [" . $row['collectionTitleShort'] . ']' : FALSE;
		$title = $this->html->stripBBCode($row['collectionTitle'] . $short);
		$thisType = $row['collectionType'];
		if($row['collectionString'])
		{
			$displayArray = $this->map->{$thisType}['display'];
			$collectionStringArray = unserialize(base64_decode($row['collectionString']));
			$extras = '';
			foreach($displayArray as $key)
			{
				if(array_key_exists($key, $collectionStringArray))
					$extras .= $this->collection->styleCollection($thisType, $key, $collectionStringArray[$key]);
			}
			$this->collections[$row['collection']] = 
				preg_replace("/{(.*)}/U", "$1", $this->html->dbToFormTidy($title . $extras));
		}
		else
			$this->collections[$row['collection']] = 
				preg_replace("/{(.*)}/U", "$1", $this->html->dbToFormTidy($title));
	}
}
?>